package com.guo.loadinganim;

import android.animation.Animator;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * 实现顶部灵动岛视图：遮蔽摄像头的最小形态
 * @author mailanglideguozhe
 * @date 2022-10-2
 */
public class TopIslandView extends LinearLayout {
    private String picture = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F18%2F20200318105813_pmbos.thumb.400_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635737969&t=70e439fdfb8a5bd6fab1dba8dfe307b7";
    private String picture1 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fww2.sinaimg.cn%2Fmw690%2F4ca1100dly1h5kfikvdmoj23uw5cb4qv.jpg&refer=http%3A%2F%2Fwww.sina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666061058&t=5974e7b7702648b0898f62a4154b6767";
    private ImageView mImageView;
    private static final String TAG = "TopBarView";
    public TopIslandView(Context context) {
        super(context);
        initView(context);
    }

    public TopIslandView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TopIslandView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        setClipChildren(false);
        View view  = LayoutInflater.from(context).inflate(R.layout.layout_top_island, this);
        mImageView = view.findViewById(R.id.image_view);
        //缓存共享图片
        Glide.with(this).asBitmap().circleCrop().load(picture).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                mImageView.setImageBitmap(resource);
            }
        });
        //设置共享元素动画(View -- > Activity)
        mImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                AnimApplication.getInstance().goToTopBarView();
            }
        });
    }

    /**
     * 接收外部传入的共享元素的宽度高度位置参数，执行入场的动效
     * @param bundle
     */
    public void enterAnimation(Bundle bundle){
        int[] originLoc = bundle.getIntArray("loc");
        int originWidth = bundle.getInt("width");
        int originHeight = bundle.getInt("height");
        mImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                Log.d(TAG, "top bar view enter animation");
                mImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                int[] latestLoc = new int[2];
                mImageView.getLocationOnScreen(latestLoc);
                int latestWidth = mImageView.getWidth();
                int latestHeight = mImageView.getHeight();
                mImageView.setPivotX(0.5f);
                mImageView.setPivotY(0.5f);
                mImageView.setScaleX(originWidth*1.0f/latestWidth);
                mImageView.setScaleY(originHeight*1.0f/latestHeight);
                mImageView.setTranslationX(latestLoc[0] - originLoc[0]);
                mImageView.setTranslationY(latestLoc[1] - originLoc[1]);
                Log.d(TAG, "translation from y = "+originLoc[1] +", to y = "+latestLoc[1]+", from x = "+ originLoc[0]+", to x = " + latestLoc[0]);
                mImageView.animate().scaleX(1).scaleY(1).translationX(0).translationY(0).setDuration(500).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {
                        Log.d(TAG, "animation start");
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        Log.d(TAG, "animation end");
                        Log.d(TAG, "start animation");
                        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_rotate);
                        mImageView.startAnimation(animation);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                });
                return false;
            }
        });
    }

    private Bundle getTransitionBundle(){
        int[] location = new int[2];
        mImageView.getLocationOnScreen(location);
        Rect rect = new Rect();
        mImageView.getLocalVisibleRect(rect);
        Bundle b = new Bundle();
        b.putIntArray("loc", location);
        b.putInt("width", rect.width());
        b.putInt("height", rect.height());
        Log.d(TAG, "origin x = "+location[0]+", y = "+location[1]+ ", width = "+rect.width()+",height = "+rect.height());
        return b;
    }

}
